from DemConfig.DtcInput import searchDidInfo
from DemConfig.DemDTCAutoConfig import DemDTCConfig
from DemConfig.DemSwcAutoConfig import DemSwcConfig
import DemConfig.DemSwcAutoConfig as DemSwcAutoConfig
from DemConfig.DemSwcCode import *

inputPath = './InputFiles/DemConfig/'
inpputDtcRequrementFile = 'DtcInfo.xlsx'
inpputOrignDemSwcFile = 'Dem_SWC_Core0_component.arxml'

ComponentsPath = '/Components'
DemSwcName = 'Dem_SWC_Core0'
RunnableName = 'RE_Dem_SWC_Core0_10ms'

SystemPath = '/System'
CompositionName = 'TopLevelComposition'

outPath = './outputFiles/DemConfig/'
outDemConfigFile = 'DtcCfg.arxml'
outDemSwcFile = 'DemSWC.arxml'
outSystemFile = 'System_DemSwcAutoMapping.arxml'

def main():
    DemDtcInfo = searchDidInfo(inputPath + inpputDtcRequrementFile)

    # config dem bsw
    DemCfg = DemDTCConfig()
    
    for DtcInfo in DemDtcInfo.dtcList:
        DemCfg.addDtc_cfg(DtcInfo)

    for DemEnCondGroupKey in DemDtcInfo.DemEnCondGroupDict:
        DemCfg.addDtcEnableConditionGroup_cfg(DemEnCondGroupKey,DemDtcInfo.DemEnCondGroupDict[DemEnCondGroupKey])

    for DemEnCond in DemDtcInfo.DemEnCondList:
        DemCfg.addDtcEnableCondition_cfg(DemEnCond)

    for DemDounce in DemDtcInfo.DemDounceList:
        DemCfg.addDebounce_cfg(DemDounce)

    DemCfg.outputXml(outPath, outDemConfigFile)
    
    #config dem swc
    # DemSwcCfg = DemSwcConfig('/Components', 'Dem_SWC_Core0')
    
    # DemSwcFile = DemSwcAutoConfig.createDemSwcArxmlFile('/Components')
    # DemSwcAutoConfig.addDemSwcComponent(DemSwcFile,DemSwcCfg)

    DemSwcFile = DemSwcAutoConfig.paraseDemSwcArxmlFile(inputPath + inpputOrignDemSwcFile,ComponentsPath)
    
    DemSwcCfg = DemSwcFile.componentDict[ComponentsPath + '/' + DemSwcName]

    DemSwcAutoConfig.addOpCycleRPort(DemSwcCfg)
    DemSwcAutoConfig.addOpCycleServerCall(DemSwcCfg,RunnableName)

    for DtcInfo in DemDtcInfo.dtcList:
        for event in DtcInfo.eventList:
            DemSwcAutoConfig.addEventRPort(DemSwcCfg,event)
            DemSwcAutoConfig.addEventStatusServerCall(DemSwcCfg,RunnableName,event)
    
    for DemEnCond in DemDtcInfo.DemEnCondList:
        DemSwcAutoConfig.addEnConditionRPort(DemSwcCfg,DemEnCond)
        DemSwcAutoConfig.addEnConditionServerCall(DemSwcCfg,RunnableName,DemEnCond)

    DemSwcFile.outputXml(outPath, outDemSwcFile)

    # #config dem swc port mapping
    systemFile = DemSwcAutoConfig.createDemSystemArxmlFile(SystemPath)

    composition = DemSwcAutoConfig.createDemCompisition(systemFile,SystemPath,CompositionName)

    DemSwcAutoConfig.addDemSwcComponentInCompostion(composition,DemSwcCfg)

    DemSwcAutoConfig.addOpCycleConnector(composition,DemSwcName)

    for DemEnCond in DemDtcInfo.DemEnCondList:
        DemSwcAutoConfig.addEnableCondConnector(composition,DemSwcName,DemEnCond)

    for DtcInfo in DemDtcInfo.dtcList:
        for event in DtcInfo.eventList:
            DemSwcAutoConfig.addEventConnector(composition,DemSwcName,event)

    systemFile.outputXml(outPath, outSystemFile)
    
    #generate dem swc code
    for DtcInfo in DemDtcInfo.dtcList:
        for event in DtcInfo.eventList:
            generateSetEventCode(event,DtcInfo.DtcNum)
    writeSetEventCode(outPath + "dem.c")
